﻿using HalconDotNet;
using Halcon二次开发.Model.储存每个Controller的单例对象;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Halcon二次开发.Controller.模板匹配;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Controller.多模板匹配;

namespace Halcon二次开发.View.多模板匹配
{
    public partial class H_PMA_S_AlignToolForm : Form
    {
        //工程文件路径
        public string filePath;
        //保存Json文件的路径
        public string jsonPath;
        //基准数据保存路径
        public string standardPath;
        //将自定义界面进行存储
        public List<UserControl> listNodes=new List<UserControl>();
        /// <summary>
        /// 每new一次就是相当于一个界面
        /// </summary>
        public Shap_S_Matching controller;
        HSmartWindowControl hw;
        //初始化
        public H_PMA_S_AlignToolForm()
        {
            InitializeComponent();
            
            controller = new Shap_S_Matching(this);
            hw = StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1;
            toolStripComboBox1.SelectedIndex = 0;

            numericUpDown3.Value = 45;
            numericUpDown4.Value = -45;
            //最小对比度
            numericUpDown2.Value = 30;
            comboBox1.SelectedIndex = 2;
            //矩形
            node1 = new Rectangle2Control();
            node1.Dock = DockStyle.Fill;
            node1.XUpDown.ValueChanged += Click;
            node1.YUpDown.ValueChanged += Click;
            node1.Length1UpDown.ValueChanged += Click;
            node1.Length2UpDown.ValueChanged += Click;
            node1.PhiUpDown.ValueChanged += Click;
            //圆形
            node2 = new CirleControl();
            node2.Dock = DockStyle.Fill;
            node2.XUpDown.ValueChanged += Click;
            node2.YUpDown.ValueChanged += Click;
            node2.RaduisUpDown.ValueChanged += Click;
            //将node进行存储
            listNodes.Add(node1);
            listNodes.Add(node2);
            //默认显示矩形
            groupBox3.Controls.Add(node1);
        }
        public void SetPathNodes(string filePath, string nodes)
        {
            this.jsonPath = filePath + "\\" + "界面数据" + "\\" + nodes + "\\" + nodes + "数据.json";
            this.filePath = (filePath + "\\" + "模板匹配文件" + "\\" + nodes + "工位模板.shm");
            this.standardPath = filePath + "\\" + "基准数据" + "\\" + nodes + "基准数据";
            //显示数据PMA
            ShowData();
        }
        /// <summary>
        /// 将保存之后的数据进行展示到界面上
        /// </summary>
        public void ShowData()
        {
            //调用存储方法
            new LoadData().LoadDatas(jsonPath,this, listNodes);
        }
        /// <summary>
        /// 画矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (controller.HideIntputImage == null ||controller.ImageIsNg())
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name+"当前图像为空!\r\n");
                return;
            }
            if (toolStripComboBox1.SelectedItem.ToString() == "旋转矩形")
            {
                controller.DrawRectangle2();

            } else if (toolStripComboBox1.SelectedItem.ToString() == "圆形") {
                controller.DrawCirle();
            }
        }

        private void H_PMAAlignToolForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            controller.ClearTwoObject(StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1.HalconWindow);
        }
        /// <summary>
        /// 搜索模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Run();
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 创建模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                controller.CreatShapeModel();
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }

        }
        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            try
            {
                controller.SaveShapeModel(filePath);
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 矩形选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //矩形参数
        public Rectangle2Control node1;
        //圆形参数
        public CirleControl node2;
        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox3.Controls.Clear();
            string str = (sender as ToolStripComboBox).Text;
            if (str == "旋转矩形")
            {
                groupBox3.Controls.Add(node1);
            }
            else if (str == "圆形")
            {
                groupBox3.Controls.Add(node2);
            }
        }

        private void Click(object sender, EventArgs e)
        {
            controller.Move(sender);
        }

        /// <summary>
        /// 单次运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Tool_Run();
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
    }
}
